﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alma.Models;
using AlmaDatabase;
using System.Diagnostics;

namespace Alma.Controllers
{
    public class ListController : Controller
    {
        //
        // GET: /List/

        public ActionResult Show(int id)
        {
            /* using (var context = new AlmaDBEntities())
             {
                 var list = from lists in context.T_List
                            where lists.ID_List == id
                            select lists;
                 return View(new ListModel(list.First().ID_List, list.First().ID_User, list.First().List_Name));
             };*/
            ListModel newComp = new ListModel();
            newComp.ID = id;
            if (Session["UserId"] != null)
                newComp.ID_User = (long)Session["UserId"];
            else
                newComp.ID_User = 0;

            using (var context = new AlmaDBEntities())
            {
                /*product*/
                var list = from product in context.Products
                           join jproduct in context.J_List_Products on product.ID equals jproduct.ID_Product
                           where jproduct.ID_List == id
                           select product;
                foreach (var pro in list)
                {
                    ProductDetailsModel productModel = new ProductDetailsModel(pro);
                    var listProd = from cart in context.J_List_Products
                                   where cart.ID_Product == pro.ID
                                   select cart;
                    productModel.Quantity = listProd.First().Quantity;
                    newComp.productList.Add(productModel);
                }
                var list2 = from lists in context.T_List
                            where lists.ID_List == id
                            select lists;
                newComp.List_Name = list2.First().List_Name;
            }
            return View(newComp);
        }

        public ActionResult Create()
        {
            return View(new ListModel());
        }

        [HttpPost]
        public ActionResult Create(ListModel list)
        {
            if (!ModelState.IsValid)
            {
                return View(list);
            }
            using (var context = new AlmaDBEntities())
            {

                T_List tList = new T_List();
                tList.ID_User = (long)Session["UserId"];
                tList.List_Name = list.List_Name;
                context.T_List.AddObject(tList);
                context.SaveChanges();
            }
            return RedirectToAction("Show", "Cart", new { id = list.ID_User });
        }

        public ActionResult Delete(int id)
        {
            using (var context = new AlmaDBEntities())
            {
                var list = from lists in context.T_List
                           where lists.ID_List == id
                           select lists;
                return View((list.First()));
            };
        }

        //
        // POST: /Section/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, T_List myList)
        {
            if (ModelState.IsValid)
            {
                using (var context = new AlmaDBEntities())
                {
                    /*Delete prods*/
                    var list0 = from prods in context.J_List_Products
                               where prods.ID_List == id
                               select prods;
                    foreach (var product in list0)
                        context.J_List_Products.DeleteObject(product);
                    context.SaveChanges();


                    var list = from lists in context.T_List
                               where lists.ID_List == id
                               select lists;
                    context.T_List.DeleteObject(list.First());
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "Cart", new { id = Session["UserId"] });
            }
            return View();
        }

        public ActionResult AddProduct(int idList, int idProduct)
        {
            try
            {
                using (var context = new AlmaDBEntities())
                {
                    J_List_Products c = new J_List_Products();
                    c.ID_List = idList;
                    c.ID_Product = idProduct;
                    c.Quantity = 1;
                    context.J_List_Products.AddObject(c);
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "Cart");
            }
            catch (Exception)
            {
                return RedirectToAction("Show", "Cart");
            }
        }

        public ActionResult DelProduct(int idList, int idProduct)
        {
            try
            {
                using (var context = new AlmaDBEntities())
                {
                    var prod = from lists in context.J_List_Products
                               where lists.ID_List == idList
                               where lists.ID_Product == idProduct
                               select lists;
                    context.J_List_Products.DeleteObject(prod.First());
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "List", new { id = idList });
            }
            catch (Exception)
            {
                return RedirectToAction("Show", "List", new { id = idList });
            }
        }

        public ActionResult More(int idProduct, int idList)
        {
            using (var context = new AlmaDBEntities())
            {
                Debug.WriteLine(idProduct+"LOL"+idList);
                var listProd = from list in context.J_List_Products
                               where list.ID_List == idList
                               where list.ID_Product == idProduct
                               select list;
                if (listProd.First().Quantity < 10000)
                    listProd.First().Quantity++;
                context.SaveChanges();
            }
            return RedirectToAction("Show", "List", new { id = idList });
        }

        public ActionResult Less(int idProduct, int idList)
        {
            using (var context = new AlmaDBEntities())
            {
                var listProd = from list in context.J_List_Products
                               where list.ID_List == idList
                               where list.ID_Product == idProduct
                               select list;
                if (listProd.First().Quantity > 0)
                    listProd.First().Quantity--;
                context.SaveChanges();
            }
            return RedirectToAction("Show", "List", new { id = idList });
        }

        public ActionResult Free(int idList)
        {
            using (var context = new AlmaDBEntities())
            {
                var list0 = from prods in context.J_List_Products
                            where prods.ID_List == idList
                            select prods;
                foreach (var product in list0)
                    context.J_List_Products.DeleteObject(product);
                context.SaveChanges();
            }
            return RedirectToAction("Show", "List", new { id = idList });
        }

        public ActionResult CartToList(int idUser, int idList)
        {
            try
            {
                using (var context = new AlmaDBEntities())
                {
                    var prod = from cart in context.Cart
                               where cart.ID_User == idUser
                               select cart;
                    foreach (var product in prod)
                    {
                        J_List_Products nProd = new J_List_Products();
                        nProd.ID_List = idList;
                        nProd.ID_Product = product.ID_Product;
                        nProd.Quantity = product.Quantity;
                        context.J_List_Products.AddObject(nProd);
                    }
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "List", new { id = idList });
            }
            catch (Exception)
            {
                return RedirectToAction("Show", "List", new { id = idList });
            }
        }

        public ActionResult ListToCart(int idUser, int idList)
        {
            try
            {
                using (var context = new AlmaDBEntities())
                {
                    var prod = from list in context.J_List_Products
                               where list.ID_List == idList
                               select list;
                    foreach (var product in prod)
                    {
                        Cart nProd = new Cart();
                        nProd.ID_User = idUser;
                        nProd.ID_Product = product.ID_Product;
                        nProd.Quantity = product.Quantity;
                        nProd.Date = DateTime.Now;
                        context.Cart.AddObject(nProd);
                    }
                    context.SaveChanges();
                }
                return RedirectToAction("Show", "Cart", new { id = idUser });
            }
            catch (Exception)
            {
                return RedirectToAction("Show", "Cart", new { id = idUser });
            }
        }
    }
}
